package com.erp.fin.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.*;
import com.erp.itm.domain.PaytypeDO;
import com.erp.itm.domain.ProjectDO;
import com.erp.itm.service.PaytypeService;
import com.erp.itm.service.ProjectService;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.system.domain.CompanyDO;
import com.erp.system.domain.UserDO;
import com.erp.system.service.CompanyService;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.fin.domain.CostapplyDO;
import com.erp.fin.service.CostapplyService;

import javax.servlet.http.HttpServletResponse;

/**
 * 费用申请
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-11-17 19:42:02
 */
 
@Controller
@RequestMapping("/fin/costapply")
public class CostapplyController  extends BaseController {
	@Autowired
	private CostapplyService costapplyService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private PaytypeService paytypeService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserService userService;
	@Autowired
	private NotifyService notifyService;


	@GetMapping()
	String Costapply(){
	    return "fin/costapply/costapply";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		if(getUser().getDeptId() !=16){
			params.put("userIdCreate",getUserId());
		}
        Query query = new Query(params);
		List<CostapplyDO> costapplyList = costapplyService.list(query);
		int total = costapplyService.count(query);
		PageUtils pageUtils = new PageUtils(costapplyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "fin/costapply/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		CostapplyDO costapply = costapplyService.get(id);
		model.addAttribute("costapply", costapply);
		List<CompanyDO> companyDOList = companyService.list(null);
		model.addAttribute("companyDOList", companyDOList);
		List<PaytypeDO> paytypeList = paytypeService.list(null);
		model.addAttribute("paytypeList", paytypeList);
		List<ProjectDO> projectList = projectService.getNotEndProjectlist();
		model.addAttribute("projectList", projectList);
	    return "fin/costapply/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( CostapplyDO costapply){
		costapply.setStatus(1);
		costapply.setGmtCreate(new Date());
		costapply.setUserIdCreate(getUserId());
		costapply.setUserNameCreate(getUser().getName());
		//查询付款方
		setInfo(costapply);
		if(costapplyService.save(costapply)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( CostapplyDO costapply){
		setInfo(costapply);
		costapplyService.update(costapply);
		return R.ok();
	}

	/**
	 * 基本信息赋值
	 * @param costapply
	 */
	private void setInfo(CostapplyDO costapply) {
		//查询付款方
		CompanyDO companyDO = companyService.get(Integer.parseInt(costapply.getPaymentComid() + ""));
		if (companyDO != null) {
			costapply.setPaymentName(companyDO.getName());
		}
		//查询付款方式
		PaytypeDO paytypeDO = paytypeService.get(costapply.getPaymentTypeid());
		if (paytypeDO != null) {
			costapply.setPaymentType(paytypeDO.getName());
		}
		//查询项目
		ProjectDO projectDO = projectService.get(costapply.getItmProjectid());
		if (projectDO != null) {
			costapply.setItmProjectnumber(projectDO.getItmNumber());
			costapply.setItmProjectname(projectDO.getName());
		}
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(costapplyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 确认
	 * @return
	 */
	@PostMapping( "/okconfirm")
	@ResponseBody
	public R okconfirm(Long id,int status){
		if(id != null && status >0){
			//修改状态
			if(costapplyService.okconfirm(id,status)>0){
				if(status == 3){
					CostapplyDO costapplyDO = costapplyService.get(id);
					//通知
					//发通知和邮件通知采购和总经理
					NotifyDO notify = new NotifyDO();
					notify.setType("9");
					notify.setTitle("费用申请");
					notify.setContent(costapplyDO.getUserNameCreate()+"申请了["+costapplyDO.getAppliAmount()+"]的金额，请及时审核！");
					List<Long> deprids = new ArrayList<>();
					deprids.add(16L);
					List<UserDO> userDOList =userService.getUsersByDeptIds(deprids);
					List<Long> userId = userDOList.stream().map(UserDO::getUserId).collect(Collectors.toList());
					Long[] userIds = userId.toArray( new Long[userId.size()]);
					notify.setUserIds(userIds);
					notify.setStatus("1");
					notify.setCreateBy(getUserId());
					notifyService.save(notify);
				}
				return R.ok();
			}
			return R.error();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		costapplyService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping("/exportData")
	@ResponseBody
	public void downloadTemplate(CostapplyDO costapply, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(costapply.getStatus() != null){
			params.put("status",costapply.getStatus());
		}
		if(StringUtils.isNotBlank(costapply.getPayee())){
			params.put("payee",costapply.getPayee());
		}
		if(StringUtils.isNotBlank(costapply.getPaymentType())){
			params.put("paymentType",costapply.getPaymentType());
		}
		if(StringUtils.isNotBlank(costapply.getItmProjectname())){
			params.put("itmProjectname",costapply.getItmProjectname());
		}
		List<CostapplyDO> costapplyDOList = costapplyService.list(params);
		ExcelUtil.exportExcel(costapplyDOList,"费用申请","费用申请",CostapplyDO.class,"费用申请.xls",response);
	}

}
